<template>
  <el-dialog
    v-model="visible"
    title="修改密码"
    width="380px"
    :close-on-click-modal="false"
    class="change-password-dialog"
    @closed="resetForm"
    :destroy-on-close="true"
  >
    <el-form
      :model="form"
      :rules="rules"
      ref="formRef"
      label-width="0"
      class="change-password-form"
    >
      <el-form-item prop="oldPassword">
        <el-input
          v-model="form.oldPassword"
          type="password"
          placeholder="旧密码"
          clearable
          class="custom-input"
        />
      </el-form-item>
      <el-form-item prop="newPassword">
        <el-input
          v-model="form.newPassword"
          type="password"
          placeholder="新密码"
          clearable
          class="custom-input"
        />
      </el-form-item>
      <el-form-item prop="confirmPassword">
        <el-input
          v-model="form.confirmPassword"
          type="password"
          placeholder="确认新密码"
          clearable
          class="custom-input"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button class="btn" @click="onCancel">取消</el-button>
      <el-button class="btn" type="primary" @click="onSubmit">确定</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, watch } from 'vue'
import { changePassword } from '@/api/auth'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'

const router = useRouter()
const visible = defineModel()
const form = ref({ oldPassword: '', newPassword: '', confirmPassword: '' })
const formRef = ref()
const errorMessage = ref('')
const rules = {
  oldPassword: [{ required: true, message: '请输入旧密码', trigger: 'blur' }],
  newPassword: [
    { required: true, message: '请输入新密码', trigger: 'blur' },
    {
      validator: (rule, value, callback) => {
        if (!value || value.length < 6) {
          callback(new Error('新密码不能少于6位'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ],
  confirmPassword: [
    { required: true, message: '请确认新密码', trigger: 'blur' },
    {
      validator: (rule, value, callback) => {
        if (value !== form.value.newPassword) {
          callback(new Error('两次输入的新密码不一致'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ]
}

function resetForm() {
  form.value.oldPassword = ''
  form.value.newPassword = ''
  form.value.confirmPassword = ''
  formRef.value?.clearValidate()
  errorMessage.value = ''
}

function onCancel() {
  visible.value = false
}

async function onSubmit() {
  await formRef.value.validate()
  const userId = localStorage.getItem('userId')
  try {
    const res = await changePassword(userId, {
      OldPassword: form.value.oldPassword,
      NewPassword: form.value.newPassword
    })
    if (res && res.Code === 200) {
      ElMessage.success('密码修改成功，请重新登录')
      visible.value = false
      setTimeout(() => {
        localStorage.removeItem('token')
        localStorage.removeItem('userId')
        localStorage.removeItem('username')
        localStorage.removeItem('isLogin')
        router.replace('/login')
      }, 2000)
    } else {
      const errorMsg = res?.Message || res?.message || '修改失败'
      ElMessage.error(errorMsg)
    }
  } catch (e) {
    if (e.response?.data) {
      const errorData = e.response.data
      const msg = errorData.Message || errorData.message || '修改失败'
      ElMessage.error(msg)
    } else {
      ElMessage.error(e.message || '修改失败')
    }
  }
}

watch(visible, (val) => {
  if (val) {
    resetForm()
  }
})
</script>

<style scoped>
.change-password-dialog .el-dialog__body {
  padding: 28px 32px 12px 32px;
}

.change-password-form {
  width: 100%;
  max-width: 280px;
  margin: 0 auto;
  margin-bottom: 0 !important;
}

.change-password-form .el-form-item {
  width: 100%;
  margin-bottom: 16px;
}

.change-password-form .el-form-item:last-child {
  margin-bottom: 0 !important;
}

/* 自定义输入框样式 */
.custom-input :deep(.el-input__wrapper) {
  background-color: #ffffff;
  border: 1px solid #e4e7ed;
  border-radius: 8px;
  box-shadow: none;
  transition: border-color 0.2s ease;
  height: 44px;
  padding: 0 16px;
}

.custom-input :deep(.el-input__wrapper:hover) {
  border-color: #c0c4cc;
}

.custom-input :deep(.el-input__wrapper.is-focus) {
  border-color: #409eff;
  box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.1);
}

.custom-input :deep(.el-input__inner) {
  color: #606266;
  font-size: 14px;
  line-height: 1.5;
}

.custom-input :deep(.el-input__inner::placeholder) {
  color: #c0c4cc;
  font-size: 14px;
}

.custom-input :deep(.el-input__suffix) {
  width: 32px;
  min-width: 32px;
}



.error-message-bar {
  color: #fff;
  background: #f56c6c;
  padding: 8px 16px;
  border-radius: 6px;
  margin-bottom: 16px;
  text-align: center;
  font-size: 15px;
}

.btn {
  min-width: 90px;
  height: 38px;
  border-radius: 6px;
  font-size: 15px;
}

.change-password-dialog .el-dialog__footer {
  margin-top: 0 !important;
}
</style> 